//
//  CircularProgressView.m
//  iiecg
//
//  Created by Shanlin on 2016/11/15.
//  Copyright © 2016年 Boundary Technology. All rights reserved.
//

#import "IKCircularProgressView.h"
#import <QuartzCore/QuartzCore.h>

#define sqrt2 1.4142135623731
#define _2sqrt2 2.8284271247462
#define sqrt2P1 0.4142135623731

#define degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式

@interface IKCircularProgress()

@property(nonatomic,weak) IKCircularProgressView *progressView;

@end

@implementation IKCircularProgress

-(void)setEndText:(NSString *)endText{
    __weak typeof(self)weakSelf = self;
    _endText = endText;
    if (_progressView) {
        [_progressView updateCircularProgress:weakSelf];
    }
}

-(void)setStartText:(NSString *)startText{
    __weak typeof(self)weakSelf = self;
    _startText = startText;
    if (_progressView) {
        [_progressView updateCircularProgress:weakSelf];
    }
}


@end


@interface IKCircularProgressView()

@property(nonatomic,strong) IKCircularProgress *circularProgress;
@property(nonatomic,strong) CAShapeLayer *progressLayer;
@property(nonatomic,strong) CAShapeLayer *progressBGLayer;
@property (nonatomic,strong)UIBezierPath* progressPath;//
@property (nonatomic,strong)UIBezierPath* progressBGPath;//

@property (nonatomic,assign) NSInteger width;//
@property (nonatomic,assign) NSInteger height;//

@end

@implementation IKCircularProgressView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)setCircularProgress:(IKCircularProgress *)circularProgress{
    __weak typeof(self)weakSelf = self;
    _circularProgress = circularProgress;
    [weakSelf updateCircularProgress:circularProgress];
}


+(instancetype)initWithFrame:(CGRect)frame circularProgress:(IKCircularProgress *)circularProgress{
    IKCircularProgressView * cpView = [[IKCircularProgressView alloc] initWithFrame:frame];
    [cpView createWithFrame:frame];
    cpView.width = frame.size.width;
    cpView.height = frame.size.height;
    cpView.circularProgress = circularProgress;
    [cpView reset];
    circularProgress.progressView = cpView;
    return cpView;
}

-(void)createWithFrame:(CGRect)frame{
    
    _progressBGLayer = [CAShapeLayer layer];
    _progressBGLayer.frame = frame;
    _progressBGLayer.fillColor =  [[UIColor clearColor] CGColor];
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = frame;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    
    [self.layer addSublayer:_progressBGLayer];
    [self.layer addSublayer:_progressLayer];
}

-(void)updateCircularProgress:(IKCircularProgress*)circularProgress{
    __weak typeof(self)weakSelf = self;
    
    if (weakSelf.subviews && [weakSelf.subviews count] >0) {
        for (UIView * v in weakSelf.subviews) {
            [v removeFromSuperview];
        }
    } 
    
    NSInteger R = weakSelf.width > weakSelf.height ? weakSelf.height:weakSelf.width;
    NSInteger pw = circularProgress.progressWidth;
    NSInteger pbgw = circularProgress.progressBGWidth;
    NSInteger poffset = pw >= pbgw ? pw : pbgw;
    //    NSInteger poffset = 0;
    NSInteger xp=0,yp=0;
    if (weakSelf.width >= weakSelf.height) {
        xp = (weakSelf.width - weakSelf.height) / 2;
    }
    if (weakSelf.width < weakSelf.height) {
        yp = (weakSelf.height - weakSelf.width) / 2;
    }
    
    _progressBGPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(weakSelf.width / 2, weakSelf.height / 2) radius:R/2 - poffset  startAngle:degreesToRadians(-225) endAngle:degreesToRadians(45) clockwise:YES];
    _progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(weakSelf.width / 2, weakSelf.height / 2) radius:R/2 - poffset   startAngle:degreesToRadians(-225) endAngle:degreesToRadians(45) clockwise:YES];
    
    _progressBGLayer.path = [_progressBGPath CGPath];
    _progressLayer.path = [_progressPath CGPath];
    
    _progressBGLayer.fillColor = [[UIColor clearColor] CGColor];
    _progressBGLayer.strokeColor = [circularProgress.progressBGColor CGColor];//指定path的渲染颜色
    _progressLayer.fillColor = [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor = [circularProgress.progressColor CGColor];//指定path的渲染颜色
    _progressBGLayer.opacity = 1; //背景同学你就甘心做背景吧，不要太明显了，透明度小一点
    
    _progressBGLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的
    _progressBGLayer.lineWidth = circularProgress.progressBGWidth;//线的宽度
    _progressLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的
    _progressLayer.lineWidth = circularProgress.progressWidth;//线的宽度
    CGFloat ylast = (R*sqrt2P1)/_2sqrt2;
    CGFloat y = R-ylast;
    UILabel * startLabel = [[UILabel alloc] initWithFrame:CGRectMake(xp + poffset, yp+y, 2*ylast, R-y)];
    UILabel * endLabel = [[UILabel alloc] initWithFrame:CGRectMake(xp + R  - 2*ylast - poffset, yp+y, 2*ylast, R-y)];
    startLabel.textColor = [UIColor whiteColor];
    endLabel.textColor = [UIColor whiteColor];
    startLabel.font = [UIFont systemFontOfSize:15];
    endLabel.font = [UIFont systemFontOfSize:15];
    startLabel.textAlignment = NSTextAlignmentCenter;
    endLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:startLabel];
    [self addSubview:endLabel];
    
    startLabel.text = circularProgress.startText;
    endLabel.text = circularProgress.endText;
    if (circularProgress.startTextColor) {
        startLabel.textColor = circularProgress.startTextColor;
    }
    if (circularProgress.endTextColor) {
        endLabel.textColor = circularProgress.endTextColor;
    }

}

-(void)setProgress:(NSInteger)progress{
    if (progress < 0 ) progress = 0;
    if (progress > 100) progress = 100;
    _progress = progress;
    if(!self.isDisableAnimate)
    {
        [CATransaction begin];
        [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
        [CATransaction setAnimationDuration:.1];
    }
    _progressLayer.strokeEnd = progress/100.0;
    if(!self.isDisableAnimate)
    {
        [CATransaction commit];
    }
}


-(void)reset{
    if(!self.isDisableAnimate)
    {
        [CATransaction begin];
        [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
        [CATransaction setAnimationDuration:.3];
    }
    _progressLayer.strokeEnd = 0;
    if(!self.isDisableAnimate)
    {
        [CATransaction commit];
    }
}

- (void)clearProgress
{
    [CATransaction begin];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:.001];
    
    _progressLayer.strokeEnd = 0;
    
    [CATransaction commit];
}
@end
